package cn.regexp.business.web.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.regexp.common.base.page.TablePageInfo;
import cn.regexp.common.web.AjaxResult;
import cn.regexp.domain.ShopGoods;
import cn.regexp.domain.ShopIngredient;
import cn.regexp.domain.ShopIngredientBook;
import cn.regexp.domain.ShopUser;
import cn.regexp.query.IngredientQuery;
import cn.regexp.service.IIngredientBookService;
import cn.regexp.service.IIngredientService;
import cn.regexp.service.IShopUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

/**
 * 食材控制器
 */
@Slf4j
@Controller
@RequestMapping("business/ingredientBook")
public class IngredientBookController {
    //模板前缀
    private static final String prefix = "business/ingredientBook/";

    @Autowired
    private IIngredientBookService ingredientBookService;
    @Autowired
    private IShopUserService userService;
    @Autowired
    private IIngredientService ingredientService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:ingredientBook:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:ingredientBook:add")
    @RequestMapping("/addPage")
    public String addPage(Model model) {
        return prefix + "add";
    }

    @RequiresPermissions("business:ingredientBook:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        QueryWrapper<ShopGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", ShopGoods.DELETE_NO);
        model.addAttribute("ingredient", ingredientBookService.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:ingredientBook:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ShopIngredientBook> query(IngredientQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<ShopIngredientBook> wrapper = new QueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(qo.getKeyword()), "goods_name", qo.getKeyword())
                .eq("is_del", ShopIngredientBook.NO);
        List<ShopIngredientBook> ingredientBookList = ingredientBookService.list(wrapper);
        ingredientBookList.forEach(item -> {
            // 查询用户名
            ShopUser user = userService.getById(item.getBookUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                item.setBookUsername(user.getUsername());
            }
            // 查询食材名称
            List<Long> ids = item.parseIngredientIds();
            List<ShopIngredient> ingredients = ingredientService.listByIds(ids);
            StringBuilder sb = new StringBuilder();
            ingredients.forEach(ingredient -> sb.append(ingredient.getIngredientName()).append(","));
            item.setIngredientName(sb.length() > 0 ? sb.substring(0, sb.length() - 1) : "");
        });
        return new TablePageInfo<>(ingredientBookList, ingredientBookService.count(wrapper));
    }

    //新增
    @RequiresPermissions("business:ingredientBook:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShopIngredientBook ingredient) {
        ingredientBookService.save(ingredient);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:ingredientBook:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopIngredientBook ingredient) {
        ingredientBookService.updateById(ingredient);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:ingredientBook:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        List<String> idList = Collections.singletonList(ids);
        idList.forEach(id -> {
            UpdateWrapper<ShopIngredientBook> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", id).set("is_del", ShopIngredientBook.YES);
            ingredientBookService.update(wrapper);
        });
        return AjaxResult.success();
    }
}
